using Asteroid.Content;
using GameFramework.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace Asteroid.Screens
{
    /// <summary>
    /// This is the menu screen.
    /// </summary>
    public class MenuScreen : Screen
    {
        /// <summary>
        /// The menu title
        /// </summary>
        private readonly Label menuTitle;

        /// <summary>
        /// The new game menu item.
        /// </summary>
        private readonly Label newGameItem;

        /// <summary>
        /// The settings menu item.
        /// </summary>
        private readonly Label settingsItem;

        /// <summary>
        /// The exit menu item.
        /// </summary>
        private readonly Label exitItem;

        /// <summary>
        /// Initializes a new instance of the <see cref="MenuScreen"/> class.
        /// </summary>
        public MenuScreen()
        {
            menuTitle = new Label
                        {
                            FontName = Fonts.MenuTitle,
                            Color = Color.Yellow,
                            Text = "Asteroid",
                            Position = new Vector2(ScreenWidth / 2, 50)
                        };

            newGameItem = new Label
                        {
                            FontName = Fonts.MenuItem,
                            Color = Color.White,
                            Text = "New Game",
                            Position = new Vector2(ScreenWidth / 2, 350)
                        };

            settingsItem = new Label
                          {
                              FontName = Fonts.MenuItem,
                              Color = Color.White,
                              Text = "Settings",
                              Position = new Vector2(ScreenWidth / 2, 450)
                          };

            exitItem = new Label
                          {
                              FontName = Fonts.MenuItem,
                              Color = Color.White,
                              Text = "Exit",
                              Position = new Vector2(ScreenWidth / 2, 550)
                          };

            Children.Add(menuTitle);
            Children.Add(newGameItem);
            Children.Add(settingsItem);
            Children.Add(exitItem);
        }

        /// <summary>
        /// Update the game menu.
        /// </summary>
        public override void Update()
        {
            base.Update();

            if (IsBackButtonPressed)
            {
                Game.Exit();
            }

            var touches = Game.Input.TouchCollection;

            if (touches.Count > 0)
            {
                TouchLocation touch = touches[0];

                if (exitItem.IsPointOverObject(touch.Position) && touch.State == TouchLocationState.Released)
                {
                    Game.Exit();
                }

                if (newGameItem.IsPointOverObject(touch.Position) && touch.State == TouchLocationState.Released)
                {
                    Game.ScreenManager.NavigateTo(new GameScreen());
                }

                if (settingsItem.IsPointOverObject(touch.Position) && touch.State == TouchLocationState.Released)
                {
                    Game.ScreenManager.NavigateTo(new SettingsScreen());
                }
            }
        }
    }
}
